{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "StartBW2"

// By ArtemQa146

wait 1000

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Busy Pedestrians mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Busy Pedestrians mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

if
    0E2D: is_game_first_start
then
    0F00: load_special_model_dff "modelsq\hole" txd "modelsq\StWork" store_to 0@
    0AF1: write_int 0@ to_ini_file "cleo\Busy Workers.ini" section "Models" key "hole"
    0F00: load_special_model_dff "modelsq\Shvelqa" txd "modelsq\StWork" store_to 0@
    0AF1: write_int 0@ to_ini_file "cleo\Busy Workers.ini" section "Models" key "Shvelqa"
end

const
    shovel = 31@
    hole = 30@
    scr_roadw = 29@
    scr_swpcar = 28@
    swpcar_particle = 27@
    pause = 1000
end

0AF0: scr_roadw = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Settings" key "Road Workers"
0AF0: scr_swpcar = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Settings" key "Sweeper Car"

0AF0: shovel = read_int_from_ini_file "cleo\Busy Workers.ini" section "models" key "Shvelqa"
0AF0: hole = read_int_from_ini_file "cleo\Busy Workers.ini" section "models" key "hole"

0AF0: swpcar_particle = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Sweeper Car" key "Particle"

timera = 0
timerb = 0


while true
wait pause
    if
        player.Defined(0)
    then
    
        if
            not $ACTIVE_INTERIOR == 0
        then
            continue
        end

            0AAA: 1@ = get_script_struct_named "RDWRQA"
            0842: 2@ = player $PLAYER_CHAR town_number
            if and
                scr_roadw == 1
                1@ == 0
                $ONMISSION == 0
                $ACTIVE_INTERIOR == 0
                not 2@ == 0
            then
                0E40: get_current_hour 3@
                if or
                    3@ >= 22
                    3@ < 5
                then
                
                    9@ = false
                    
                    10@ = 0
                    while 0EA8: get_any_car_no_save_recursive 10@ progress_to 10@ car_to 0@
                        046C: 7@ = car 0@ driver
                        0E12: get_vehicle 0@ subclass_to 3@
                        if
                            00DF:   actor $PLAYER_ACTOR driving
                        then
                            8@ = 80.0
                            if 02CA:   car 0@ bounding_sphere_visible
                            then 8@ = 100.0
                            end
                            04C4: store_coords_to 4@ 5@ 6@ from_actor $PLAYER_ACTOR with_offset 0.0 120.0 0.0
                            1@ = 30.0
                        else
                            8@ = 50.0
                            if
                                02CA:   car 0@ bounding_sphere_visible
                            then
                                break
                            end
                            04C4: store_coords_to 4@ 5@ 6@ from_actor $PLAYER_ACTOR with_offset 0.0 0.0 0.0
                            1@ = 100.0
                        end
                        02E3: 2@ = car 0@ speed
                        if and
                            2@ > 7.0
                            3@ == 0
                            not 7@ == -1
                            not is_car_script_controlled 0@
                            not locate_char_distance_to_car $Player_actor car 0@ radius 8@
                            locate_char_distance_to_car $Player_actor car 0@ radius 130.0
                            0EEB: locate_car_distance_to_coordinates 0@ pos 4@ 5@ 6@ radius 1@
                        then
                            089F: get_actor 7@ pedtype_to 3@
                            if or
                                3@ == 4
                                3@ == 5
                            then
                                0407: store_coords_to 1@ 2@ 3@ from_car 0@ with_offset 2.0 7.0 10.0
                                0407: store_coords_to 4@ 5@ 6@ from_car 0@ with_offset -2.0 -7.0 10.0
                                02CE: 3@ = ground_z_at 1@ 2@ 3@
                                02CE: 6@ = ground_z_at 4@ 5@ 6@
                                if
                                    0025:   3@ > 6@  // (float)
                                then
                                    0063: 3@ -= 6@  // (float)
                                    0087: 6@ = 3@ // (float)
                                else
                                    0063: 6@ -= 3@  // (float)
                                    0087: 3@ = 6@ // (float)
                                end
                                
                                if and
                                    3@ < 0.1
                                    6@ < 0.1
                                then
                                    00AA: store_car 0@ position_to 1@ 2@ 3@
                                    0174: 4@ = car 0@ Z_angle
                                    4@ += 180.0
                                    00A6: destroy_car 0@
                                    9@ = true
                                    break
                                end
                            end
                        end
                    end
                    if
                        9@ == true
                    then
                        0A92: stream_custom_script "Road Workers.qa" 1@ 2@ 3@ 4@ shovel hole 
                        wait 2000
                    end
                end
            end
            
            
            
            
            wait pause
            
            0842: 2@ = player $PLAYER_CHAR town_number
            if and
                scr_swpcar == 1
                $ONMISSION == 0
                $ACTIVE_INTERIOR == 0
                not 2@ == 0
            then
                0E40: get_current_hour 3@
                0D59: get_current_weather_to 2@
                if and
                    3@ >= 5
                    3@ < 10
                    not 2@ == 8
                    not 2@ == 16
                then

                    0EB2: get_offset_from_camera_in_world_coords 0.0 -50.0 0.0 store_to 1@ 2@ 3@
                    if and
                        02C1: store_to 1@ 2@ 3@ car_path_coords_closest_to 1@ 2@ 3@
                        not  locate_char_distance_to_coordinates $PLAYER_ACTOR pos 1@ 2@ 3@ radius 30.0
                        locate_char_distance_to_coordinates $PLAYER_ACTOR pos 1@ 2@ 3@ radius 50.0
                    then
                        0AAA: 0@ = get_script_struct_named "SWPCAR"
                        if
                            0@ == 0
                        then
                            0A92: stream_custom_script "Sweeper Car.qa" 1@ 2@ 3@ swpcar_particle
                            wait 2000
                        end
                    end
                end
            end

    end
end

